clear;

load JTPA_CATE_UNION_TU_1_100

TUteJTPAlb_1_100=TUteJTPAlb;
TUteJTPAub_1_100=TUteJTPAub;

load JTPA_CATE_UNION_TU_101_200

TUteJTPAlb_101_200=TUteJTPAlb;
TUteJTPAub_101_200=TUteJTPAub;

load JTPA_CATE_UNION_TU_201_300

TUteJTPAlb_201_300=TUteJTPAlb;
TUteJTPAub_201_300=TUteJTPAub;

load JTPA_CATE_UNION_TU_301_400

TUteJTPAlb_301_400=TUteJTPAlb;
TUteJTPAub_301_400=TUteJTPAub;

load JTPA_CATE_UNION_TU_401_500

TUteJTPAlb_401_500=TUteJTPAlb;
TUteJTPAub_401_500=TUteJTPAub;

load JTPA_CATE_UNION_TU_501_593

TUteJTPAlb_501_593=TUteJTPAlb;
TUteJTPAub_501_593=TUteJTPAub;

TUlb = [TUteJTPAlb_1_100;TUteJTPAlb_101_200;TUteJTPAlb_201_300;TUteJTPAlb_301_400;TUteJTPAlb_401_500;TUteJTPAlb_501_593];
TUub = [TUteJTPAub_1_100;TUteJTPAub_101_200;TUteJTPAub_201_300;TUteJTPAub_301_400;TUteJTPAub_401_500;TUteJTPAub_501_593];

load JTPA_CATE_UNION_INIT

TUteJTPAlb = zeros(n,10);
TUteJTPAub = zeros(n,10);
for ii = 1:n
    TUteJTPAlb(ii,:) = TUlb(VUnique == V(ii),:);
    TUteJTPAub(ii,:) = TUub(VUnique == V(ii),:);
end

% Sort the effects
plres = [teplJTPA steplJTPA];
plres = sortrows(plres,1);

fsreslb = [teplJTPA TUteJTPAlb];
fsreslb = sortrows(fsreslb,1);
fsresub = [teplJTPA TUteJTPAub];
fsresub = sortrows(fsresub,1);

% Figures of effects
figure; plot(1:n,plres(:,1),'b',1:n,plres(:,1)-1.96*plres(:,2),'r',1:n,plres(:,1)+1.96*plres(:,2),'r',1:n,zeros(n,1),'k');
title('Post-Lasso Pointwise 95% CI for CATE(x)');
saveas(gcf,'JTPACATE_PL','pdf');

for ii = 1:10
    figure; plot(1:n,plres(:,1),'b',1:n,fsreslb(:,1+ii),'r',1:n,fsresub(:,1+ii),'r',1:n,zeros(n,1),'k');
    title(sprintf('FS(%d) Pointwise 95% CI for CATE(x)',ii));
    filename = sprintf('JTPACATE_FS%d',ii);
    saveas(gcf,filename,'pdf');
end

% Display TU Wald tests
fprintf(' & Wald & df & p-value  \\\\ \n');
fprintf('TU(0)  & %6.4f  & %d & %6.4f  \\\\ \n',WPL,numel(testpl),pvalPL);
for ii = 1:10
    fprintf('TU(%d)  & %6.4f  & %d & %6.4f  \\\\ \n',ii,FSW(ii),FSdf(ii),FSpval(ii));
end

